{extend name="base" /}
<!-- page specific plugin styles -->
{block name="plugin-styles"}{/block}
<!-- inline styles related to this page -->
{block name="inline-styles"}{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="row">

    <div class="col-xs-6">
        <h3 class="header smaller lighter green">领导返利规则</h3>
        <form class="form-horizontal" id="rewardTeam">
            <div class="form-group">
                <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 领导积分返利金额 </label>
                <div class="col-sm-6">
                    <div class="input-group">
                        <input type="text" name="value" placeholder="" class="form-control" value='{$leader}'>
                        <span class="input-group-addon">元/件</span>
                    </div>
                </div>
                <div class="col-sm-3">
                    <button type="submit" class="btn btn-sm btn-primary">保存</button>
                </div>
            </div>
        </form>
    </div>
</div>


<h3 class="header smaller lighter green">团队返利规则</h3>
<div class="row">
    <div class="col-xs-8 col-xs-offset-2">
        <div class="tableTools-container text-right">
            <button type="button" class="btn btn-sm btn-primary" onclick="addTr()">新增</button>
            <button type="button" class="btn btn-sm btn-primary" onclick="save()">保存</button>
        </div>
        <table class="table table-bordered" id="myTable">
            <thead>
                <tr>
                    <th>订单商品数量</th>
                    <th>奖励金额(元/件)</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {foreach $team as $key=>$val}
                <tr>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">满</span>
                            <input class="form-control" type="text" name="key[]" value='{$key}' placeholder="请输入数量">
                        </div>
                    </td>
                    <td>
                        <div class="input-group">
                            <input class="form-control" type="text" name="value[]" value='{$val}' placeholder="请输入金额">
                            <span class="input-group-addon">元/件</span>
                        </div>
                    </td>
                    <td><button type="button" class="btn btn-sm btn-danger" onclick="delTr(this)">删除</button></td>
                </tr>
                {/foreach}
            </tbody>
        </table>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- page specific plugin scripts -->
{block name="plugin-scripts"}{/block}
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
    // 订单返利规则
    $("#rewardOrder").ajaxForm({
        url:"/admin/rebate/person_edit",
        type:"POST",
        dataType:"json",
        success: function(json){
            if(json.returnCode==1){
                alert("保存成功");
            }else{
                alert(json.returnMsg);
            }
        }
    });
    // 领导返利规则
    $("#rewardTeam").ajaxForm({
        url:"/admin/rebate/leader_edit",
        type:"POST",
        dataType:"json",
        success: function(json){
            if(json.returnCode==1){
                alert("保存成功");
                window.location='';
            }else{
                alert(json.returnMsg);
            }
        }
    });

    // 添加行
    function addTr(){
        var _tr =
        '<tr>\
            <td>\
                <div class="input-group">\
                    <span class="input-group-addon">满</span>\
                    <input class="form-control" type="text" name="key[]" placeholder="请输入数量">\
                </div>\
            </td>\
            <td>\
                <div class="input-group">\
                    <input class="form-control" type="text" name="value[]" placeholder="请输入金额">\
                    <span class="input-group-addon">元/件</span>\
                </div>\
            </td>\
            <td><button type="button" class="btn btn-sm btn-danger" onclick="delTr(this)">删除</button></td>\
        </tr>';
        $("#myTable").append(_tr);
    }

    // 删除行
    function delTr(e) {
        // body...
        $(e).parents('tr').remove();
    }

    //保存
    function save(argument) {
        // body...
        var keys=[];
        var values=[];
        $("#myTable tbody tr").each(function(index) {
            // body...
            var _key=$(this).find('input[name="key[]"]').val();
            var _value=$(this).find('input[name="value[]"]').val();
            
            if(_key !="" && _value!=""){
            	
                keys.push(_key);
                values.push(_value);
            }
        });
        $.ajax({
            url: '/admin/rebate/team_edit',
            type: 'POST',
            dataType: 'json',
            data: {
                type: 1,
                keys: keys ,
                values:values
            },
        })
        .done(function(json) {
            console.log("success");
            if(json.returnCode==1){
                alert("保存成功");
                window.location='';
            }else{
                alert(json.returnMsg);
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
        
    }
</script>
{/block}